import json

from flask import render_template,request,jsonify
from . import app_images

import os
from frameworks.cores.Config import *
from frameworks.utils.JsonResult2 import *
from frameworks.cores.LoadClass import *

basepath = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
config = Config(basepath)

apimap = config.getApi()
env = apimap["base"]
print(env)
from flask_session import Session

jsonResult = JsonResult2()

loader = LoadClass()

@app_images.route('/soft_package/list')
def soft_package_list():
    return render_template('app_images/soft_package_list.html')

@app_images.route(env+"/soft_package/update",methods=['POST'])
def soft_package_update():
    response = request.get_data(as_text=True)
    rs = loader.load("controller.app_images.SoftPackageController", "SoftPackageController", {
        "func": "update",
        "data": json.loads(response)
    })
    return jsonResult.setOk([], "更新成功")

@app_images.route(env+"/soft_package/delete",methods=['GET'])
def soft_package_delete():
    id = request.args.get("id",0)
    print(id)
    rs = loader.load("controller.app_images.SoftPackageController", "SoftPackageController", {
        "func": "delete",
        "data": {"id":id}
    })
    return jsonResult.setOk([], "删除成功")

@app_images.route(env+"/soft_package/add",methods=['POST'])
def soft_package_add():
    response = request.get_data(as_text=True)
    rs = loader.load("controller.app_images.SoftPackageController", "SoftPackageController", {
        "func": "add",
        "data": json.loads(response)
    })
    return jsonResult.setOk([], "添加成功")

@app_images.route(env+"/soft_package/form",methods=['GET', 'POST'])
def soft_package_form():
    model = request.args.get("model", "school")
    id = request.args.get("id", "0")
    obj = loader.load("controller.app_images.SoftPackageController","SoftPackageController",{
        "func":"getRowData",
        "fields":["id","soft_name","version","uri","dockerfile"],
        "id": id
    })
    fields = loader.load("controller.app_images.SoftPackageController","SoftPackageController",{
        "func":"getMap",
        "fields":["id","soft_name","version","uri","dockerfile"]
    })
    return render_template(
        'app_images/soft_package_form.html',
        obj=obj[0],
        fields=fields
    )

@app_images.route(env+"/soft_package/formadd",methods=['GET', 'POST'])
def soft_package_formadd():
    model = request.args.get("model", "school")
    fields = loader.load("controller.app_images.SoftPackageController","SoftPackageController",{
        "func":"getMap",
        "fields":["id","soft_name","version","uri","dockerfile"]
    })
    return render_template(
        'app_images/soft_package_formadd.html',
        fields=fields
    )

@app_images.route(env+"/soft_package/cols",methods=['GET','POST'])
def soft_package_cols():
    rs = loader.load("controller.app_images.SoftPackageController", "SoftPackageController", {
        "func":"getColumns",
        "fields":["id","soft_name","version","uri","dockerfile","ctime","mtime"]
    })
    return jsonResult.setOk(rs,"获取成功")

@app_images.route(env+"/soft_package/page",methods=['GET'])
def soft_package_page():
    num = request.args.get("limit",10)
    page = request.args.get("page",1)
    data = loader.load("controller.app_images.SoftPackageController", "SoftPackageController", {
        "func": "getData",
        "fields": ["id","soft_name","version","uri","dockerfile","ctime","mtime"],
        "page": page,
        "num": num,
        "orderfield": "id",
        "asc": "asc"
    })
    return json.dumps(data)